package net.neptech.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import net.neptech.model.dto.AppointmentDTO;
import net.neptech.model.dto.PopQueueDTO;
import net.neptech.model.sys.User;
import net.neptech.model.vo.AppointmentVo;
import net.neptech.model.vo.PatientAppVo;
import net.neptech.repository.AppointmentRepository;
import net.neptech.model.appointment.Appointment;
import net.neptech.service.DelayQueueService;
import net.neptech.service.IAppointmentService;
import net.neptech.service.impl.DelayQueueServiceImpl;
import net.neptech.vm.ResponseResult;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
@Tag(name = "预约挂号相关")
public class AppointmentController {

 private final IAppointmentService appointmentService;
 private final DelayQueueServiceImpl delayQueueService;

    public AppointmentController(IAppointmentService appointmentService, DelayQueueServiceImpl delayQueueService) {
        this.delayQueueService = delayQueueService;
        this.appointmentService = appointmentService;
    }


    /**
     * 预约接口
     * @param appointmentDTO
     * @return
     */

    @PostMapping("/appointment")
    @Operation(summary = "预约挂号")
    public ResponseResult<Appointment> appointment(@RequestBody AppointmentDTO appointmentDTO){
        return appointmentService.appointment(appointmentDTO);
    }

    /**
     * 压力测试 高并发
     * @return
     */

    @GetMapping("/test")
    public ResponseResult<Appointment> appointment(
            @RequestParam("staffId") Long staffId,
            @RequestParam("clnicId") Long clnicId){

        // 随机数
        Random random = new Random();
        // 随机数范围
        int min = 1;
        int max = 1000;
        // 生成随机数
        int randomNum = random.nextInt(max - min + 1) + min;
        log.info(randomNum+"随机数");

        AppointmentDTO appointmentDTO = new AppointmentDTO();
        /**随机id
         * 随机id
         */
        appointmentDTO.setUserId((long) randomNum);
        appointmentDTO.setStaffId(staffId);
        appointmentDTO.setPatientId(1L);
        appointmentDTO.setTypeId(1);
        appointmentDTO.setClnicId(clnicId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            appointmentDTO.setBookedAt(sdf.parse("2025-8-18"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return appointmentService.appointment(appointmentDTO);
    }
    /**
     * 取消预约按钮  还未支付
     *
     */
    @PostMapping("/cancel")
    @Operation(summary ="取消预约")
    //注意 dto时间要满足格式
    public ResponseResult<Boolean> cancelAppointment(@RequestBody AppointmentDTO appointmentDTO){
        return appointmentService.concelAppointment(appointmentDTO);
    }


    //查询预约信息

    @GetMapping("/getMyAppointments")
    @Operation(summary = "查询自己的历史预约信息")
    public ResponseResult<List<AppointmentVo>> getAppointmentList(@RequestParam("userId")Long userId) {
       return appointmentService.getAppointmentList(userId);
    }

    @GetMapping("/updateStatusByOrderId")
    @Operation(summary ="根据订单修改状态")
    public ResponseResult<Void> updateStatus(@RequestParam("orderId")Long orderId,@RequestParam("status")Byte status) {
        return appointmentService.updateAppointmentStatus(orderId,status);
    }

    @GetMapping("/testDtl")
    @Operation(summary ="测试延迟队列")
    public ResponseResult<Void> testDtl() {

        Appointment appointment = new Appointment();
        appointment.setId(1957633186428817409L).setStatus((byte) 0);
        String delayMessageId = delayQueueService.sendDelayMessage(
                "ORDER_TIMEOUT",
                appointment,
                1
        );
        System.out.println(delayMessageId);
        return ResponseResult.success();
    }

    @PostMapping("/getPatientApp")
    @Operation(summary ="根据医生id分页查询预约信息")

    public ResponseResult<Page<Appointment>> getPatientApp(@RequestBody AppointmentDTO appointmentDTO) {

        return appointmentService.getPatientApp(appointmentDTO);
    }
    /**
     * 获取自己当天的队列信息
     */
//    @GetMapping("/getMyQueue")
//    @Operation(summary ="获取自己当天的号队列信息")
//    public ResponseResult<List<PatientAppVo>> getMyQueue(@RequestParam("userId")Long userId) {
//        return appointmentService.getMySlotQueue(userId);
//    }
    /**
     * 排队
     */
    @PostMapping("/putQueue")
    @Operation(summary ="排队")
    public ResponseResult<Void> putQueue(@RequestBody AppointmentDTO appointmentDTO){

        return appointmentService.putSlotQueue(appointmentDTO);
    }

    /**
     * 根据当天时间 获取获取挂号队列信息
     */
    @GetMapping("/getQueue")
    @Operation(summary ="根据当天时间 获取获取挂号队列")
    public ResponseResult<List<AppointmentDTO>> getQueue(@RequestParam("staffId")Long staffId,@RequestParam("clnicId")Long clnicId) {

        return appointmentService.getSlotQueue(staffId,clnicId);
    }
    /**
     * 修改患者状态
     */
    @PostMapping("/updatePatientAppStatus")
    @Operation(summary ="修改患者预约状态到已完成")
    public ResponseResult<Void> updatePatientAppStatus(@RequestBody PopQueueDTO popQueueDTO) {
        return appointmentService.updatePatientAppStatus(popQueueDTO);
    }

    @GetMapping("callSlot")
    @Operation(summary ="叫号")
    public ResponseResult<Void> callSlot(@RequestParam("id") Long id) {
        return appointmentService.callSlot(id);
    }



}
